package com.situ.mymall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.situ.mymall.mapper.ProductMapper;
import com.situ.mymall.pojo.entity.Product;
import com.situ.mymall.pojo.query.ProductQuery;
import com.situ.mymall.service.IProductService;
import com.situ.mymall.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public PageResult list(ProductQuery productQuery) {
        PageHelper.startPage(productQuery.getPage(), productQuery.getLimit());
        List<Product> list = productMapper.list(productQuery);
        PageInfo pageInfo = new PageInfo(list);
        int totalCount = (int) pageInfo.getTotal();

        return PageResult.ok(totalCount,list);
    }

    /*insert会将数据表所有字段都新增。未手动设置值得，都会被null替代，包括默认值
      insertSelective只新增设置的参数，默认值未修改不会被替换*/
    @Override
    public void add(Product product) {
        productMapper.insertSelective(product);
    }

    @Override
    public Product selectById(Integer id) {
        return productMapper.selectById(id);
    }

    @Override
    public void update(Product product) {
        productMapper.updateByPrimaryKeySelective(product);
    }

    @Override
    public void deleteById(Integer id) {
        productMapper.deleteById(id);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        productMapper.deleteAll(ids);
    }
}
